package com.ziq.zcamp.api.service

class ApiUrlHelper private constructor(){

    companion object {
        val instance = SingletonHolder.holder
    }

    private object SingletonHolder {
        val holder= ApiUrlHelper()
    }

    private var domain = mutableMapOf<String, String>()

    //默认域名
    private val DEFAULT_DOMAIN = mapOf(
        "HOST" to BASE_URL ,
        "UPLOAD_HOST" to BASE_URL ,
        "MAIDIAN_HOST" to BASE_URL ,
        "H5_HOST" to BASE_URL ,
        "H5_APP_HOST" to H5_BASE_URL ,
    )


    init {
        domain.putAll(DEFAULT_DOMAIN)
    }

    fun getUrl(url: String): String {
        if(url.startsWith("http:") || url.startsWith("https:")){
            return url
        }

        val tempUrl = url
        val key = tempUrl.splitHost()

        return key?.let {
            val domainForKey = domain.get(it) ?: ""
            val realUrl = tempUrl?.replace(key, domainForKey)
            realUrl
        } ?: url
    }

}

private fun String?.splitHost(): String? {
    return if (this != null && this.contains("/")) this.split("/")[0] else this
}